package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VideoService;
import com.atguigu.vod.utils.ConstantYmlUtils;
import com.atguigu.vod.utils.InitVodClient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VideoService videoService;


    @ApiOperation(value = "上传视频到VOD")
    @PostMapping("/uploadVideo")
    public R uploadVideo(@RequestPart("file") MultipartFile file){

        String videoId = videoService.upLoadVideo(file);

        return R.ok().data(videoId);

    }

    //根据视频id删除阿里云视频
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeVideoById(@PathVariable String id){

        DefaultAcsClient client = InitVodClient.initVodClient(ConstantYmlUtils.ACCESS_KEY_ID, ConstantYmlUtils.ACCESS_KEY_SECRET);

        //创建删除对象
        DeleteVideoRequest request = new DeleteVideoRequest();

        //quest 写入id
        request.setVideoIds(id);
        //电泳初始化对象
        try {
            DeleteVideoResponse response = client.getAcsResponse(request);

            return  R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }

    }



    @ApiOperation(value = "根据id批量删除VOD视频")
    @DeleteMapping("/removeBatchVideo")
    public R deleteBatch(@RequestParam("videoList")List<String> videoList){

        Boolean removeBatch = videoService.removeVideoBatch(videoList);
        return R.flagCheck(removeBatch);

    }

    //根据视频id获取视频凭证
    @ApiOperation(value = "根据视频id获取视频凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){

        DefaultAcsClient client = InitVodClient.initVodClient(ConstantYmlUtils.ACCESS_KEY_ID,
                ConstantYmlUtils.ACCESS_KEY_SECRET);
        //创建获取凭证request和response对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        //向request设置视频id
        request.setVideoId(id);
        //调用方法得到凭证
        GetVideoPlayAuthResponse response = null;
        try {
            response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return  R.ok().data("playAuth", playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "获取凭证失败" + e.getMessage());
        }



    }


}
